﻿using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;
using kxjsj.Model;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace kxjsj
{
    /// <summary>
    /// 生成PDF文档操作类
    /// 2011.10.04 严明
    /// </summary>
    class PDFCreator
    {
        static string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + System.Windows.Forms.Application.StartupPath + "\\data\\kxjsj.mdb";
        static Font fontHei = null;                 //黑体
        static Font fontSong = null;                //宋体
        static Font fontSmallSong = null;           //宋体,小字
        static BaseFont bfHei = null;               //粗黑体
        static BaseFont bfSong = null;              //粗宋体
        static float smallCellHeight = 30;          //较小表格高度
        static float normalCellHeight = 40;         //通用表格高度
        static float tableBefore = 30;               //标题与表格的距离
        static Project project = new Project();

        /// <summary>
        /// 生成PDF文件
        /// </summary>
        /// <param name="inner_id"></param>
        /// <param name="isNull">是否为空表</param>
        /// <returns></returns>
        public static bool Create(string inner_id, bool isNull)
        {
            string path = System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + ".pdf";
            Document doc = null;
            LoadBasic(inner_id, isNull, -1, path, ref doc);

            //创建项目基本情况
            CreateProjectBasic(ref doc, project);

            doc.NewPage();
            //创建项目简介
            CreateProjectIntro(ref doc, project);

            doc.NewPage();
            //创建主要技术创新点
            CreateProjectInnovation(doc, project);

            doc.NewPage();
            //创建项目技术内容
            CreateProjectContent(ref doc, project);

            doc.NewPage();
            //创建项目应用情况
            CreateProjectAppIntro(ref doc, project);

            doc.NewPage();
            //创建社会、经济效益
            CreateProjectSocialAndEconomic(ref doc, project);

            doc.NewPage();
            //创建主要完成人情况表
            CreateAuthor(ref doc, project.AuthorList);

            doc.NewPage();
            //创建主要完成单位情况及意见表
            CreateUnit(ref doc, project.UnitList);

            doc.NewPage();
            if (!isNull)
            {
                //创建证明材料
                CreateAttachment(ref doc, project);
            }
            else
            {
                CreateKZMCL(ref doc, project);
            }

            //创建附件目录
            CreateAccessory(ref doc, project.AccessoryList);

            //创建附件图片
            CreateImages(ref doc, project, inner_id);



            //doc.NewPage();
            //创建完成人确认表
            //CreateSort(doc);

            //关闭document
            doc.Close();

            //打开pdf,看效果
            try
            {
                Process.Start(path);
            }
            catch
            {
                MessageBox.Show("检测到有PDF阅读器正在运行，请关闭PDF阅读器后重新执行此操作。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return true;
        }

        /// <summary>
        /// 按项目生成逐个PDF文件
        /// </summary>
        /// <param name="inner_id"></param>
        /// <returns></returns>
        public static void CreatePdfFiles(string inner_id)
        {
            string projectPath = System.Windows.Forms.Application.StartupPath + "\\评审材料\\" + inner_id + "\\";
            string errorMessage = string.Empty;
            Project project = GetProject(inner_id, ref errorMessage);
            LoadFont();
            Document doc = null;

            //创建项目基本情况
            doc = LoadBasic(projectPath + "001、项目基本情况.pdf");
            CreateProjectBasic(ref doc, project);
            doc.Close();

            //创建项目简介
            doc = LoadBasic(projectPath + "002、项目简介.pdf");
            CreateProjectIntro(ref doc, project);
            doc.Close();

            //创建主要技术创新点
            doc = LoadBasic(projectPath + "003、主要技术创新点.pdf");
            CreateProjectInnovation(doc, project);
            doc.Close();

            //创建项目技术内容
            doc = LoadBasic(projectPath + "004、项目技术内容.pdf");
            CreateProjectContent(ref doc, project);
            doc.Close();

            //创建项目应用情况
            doc = LoadBasic(projectPath + "005、项目应用情况.pdf");
            CreateProjectAppIntro(ref doc, project);
            doc.Close();

            //创建社会、经济效益
            doc = LoadBasic(projectPath + "006、社会、经济效益.pdf");
            CreateProjectSocialAndEconomic(ref doc, project);
            doc.Close();

            string tempPath = string.Empty;
            //创建主要完成人情况表
            tempPath = CreateDirectory(projectPath + "007、主要完成人情况表\\");
            for (int i = 0; i < project.AuthorList.Count; i++)
            {
                doc = LoadBasic(tempPath + (i + 1).ToString().PadLeft(3, '0') + "、" + project.AuthorList[i].Name + ".pdf");
                CreateSingleAuthor(ref doc, project.AuthorList[i]);
                doc.Close();
            }

            //创建主要完成单位情况及意见表
            tempPath = CreateDirectory(projectPath + "008、主要完成单位情况表\\");
            for (int i = 0; i < project.UnitList.Count; i++)
            {
                doc = LoadBasic(tempPath + (i + 1).ToString().PadLeft(3, '0') + "、" + project.UnitList[i].Name + ".pdf");
                CreateSingleUnit(ref doc, project.UnitList[i]);
                doc.Close();
            }

            //创建推荐意见
            doc = LoadBasic(projectPath + "009、推荐单位意见.pdf");
            RecommendOpinion ro = new RecommendOpinion();
            ro = db_helper.get_tjyj(constr, inner_id);
            Create推荐意见(ref doc, ro);
            doc.Close();
            //创建证明材料
            int count = 0;

            List<string> titleList = new List<string>();
            List<System.Drawing.Image> imageList = new List<System.Drawing.Image>();

            GetAttachmentList(out titleList, out imageList, project);

            tempPath = CreateDirectory(projectPath + "010、证明材料\\");
            for (int i = 0; i < 9; i++)
            {
                if (imageList.Count > 0)
                {
                    if (imageList[i] == null)
                    {
                        continue;
                    }
                doc = LoadBasic(tempPath + (i + 1).ToString().PadLeft(3, '0') + "、" + titleList[i] + ".pdf");
                CreateSingleAttachment(ref doc, titleList[i], imageList[i], ref count);
                doc.Close();
                }
            }

            ////创建附件目录
            //CreateAccessory(ref doc, project.AccessoryList);
            //doc = LoadBasic(projectPath + "011、附件目录.pdf");
            //CreateAccessory(ref doc, project.AccessoryList);
            ////CreateImages(ref doc, project);
            //doc.Close();
            ////创建附件图片
            List<string> orderList = new List<string>();
            List<string> titleList1 = new List<string>();
            List<System.Drawing.Image> imageList1 = new List<System.Drawing.Image>();
            GetAccessoryList(out orderList, out titleList1, out imageList1, project);
            tempPath = CreateDirectory(projectPath + "011、附件目录\\");
            for (int i = 0; i < orderList.Count; i++)
            {
                //if (imageList[i] == null)
                //{
                //    continue;
                //}
                doc = LoadBasic(tempPath + orderList[i].PadLeft(3, '0') + "、" + titleList1[i] + ".pdf");
                System.Drawing.Bitmap bitmap = GetAccessoryImage(inner_id, orderList[i].ToString(), ref errorMessage);
                CreateSingleAttachment(ref doc, titleList1[i], bitmap, ref count);
                doc.Close();
            }
            GC.Collect();
        }
        private static void GetAccessoryList(out List<string> orderList, out List<string> titleList, out List<System.Drawing.Image> imageList, Project project)
        {
            orderList = new List<string>();
            titleList = new List<string>();
            imageList = new List<System.Drawing.Image>();
            for (int i = 0; i < project.AccessoryList.Count; i++)
            {
                orderList.Add(project.AccessoryList[i].Order.ToString());
                titleList.Add(project.AccessoryList[i].Name);
                imageList.Add(project.AccessoryList[i].Image);
            }
        }
        private static void GetAttachmentList(out List<string> titleList, out List<System.Drawing.Image> imageList, Project project)
        {
            titleList = new List<string>();
            imageList = new List<System.Drawing.Image>();

            if (project.Attachment != null && project.Attachment.Type == "应用技术成果")
            {
                titleList.AddRange(new string[] { "成果登记证书", "完成人排序确认表", "评价报告封面", "专家评价意见", "专家评价意见第2页", "专家签字表", "专家签字表第2页", "应用证明", "应用证明第2页" });
                imageList.AddRange(new System.Drawing.Image[] { project.Attachment.File1, project.Attachment.File2, project.Attachment.File3, project.Attachment.File4, project.Attachment.File7, project.Attachment.File5, project.Attachment.File8, project.Attachment.File6, project.Attachment.File9 });
            }
            else if (project.Attachment != null && project.Attachment.Type == "标准成果")
            {
                titleList.AddRange(new string[] { "批准发布函", "批准发布函第2页", "编制说明", "审定会纪要", "审定会纪要第2页", "审定专家签字表", "审定专家签字表第2页", "标准起草人", "完成人排序确认表" });
                imageList.AddRange(new System.Drawing.Image[] { project.Attachment.File1, project.Attachment.File7, project.Attachment.File2, project.Attachment.File3, project.Attachment.File8, project.Attachment.File4, project.Attachment.File9, project.Attachment.File5, project.Attachment.File6 });
            }
            else
            {
                return;
            }
        }

        private static string CreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }

        #region 将项目技术内容的PDF文件合并后,生成完整的推荐书PDF
        /// <summary>
        /// 将项目技术内容的PDF文件合并后,生成完整的推荐书PDF
        /// </summary>
        /// <param name="inner_id"></param>
        /// <returns></returns>
        public static bool CreatePdfContainsPdf(string inner_id)
        {
            string path = System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + ".pdf";
            string path1 = System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "-1" + ".pdf";
            Document doc = null;
            LoadBasic(inner_id, false, -1, path1, ref doc);

            //创建项目基本情况
            CreateProjectBasic(ref doc, project);

            doc.NewPage();
            //创建项目简介
            CreateProjectIntro(ref doc, project);

            doc.NewPage();
            //创建主要技术创新点
            CreateProjectInnovation(doc, project);

            doc.NewPage();
            //创建项目技术内容
            //CreateProjectContent(ref doc, project);
            doc.Close();

            string contentPdfPath = CreateContentPdf(inner_id);
            string path2 = System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "-2" + ".pdf";
            doc = null;
            LoadBasic(inner_id, false, -1, path2, ref doc);

            doc.NewPage();
            //创建项目应用情况
            CreateProjectAppIntro(ref doc, project);

            doc.NewPage();
            //创建社会、经济效益
            CreateProjectSocialAndEconomic(ref doc, project);

            doc.NewPage();
            //创建主要完成人情况表
            CreateAuthor(ref doc, project.AuthorList);

            doc.NewPage();
            //创建主要完成单位情况及意见表
            CreateUnit(ref doc, project.UnitList);

            doc.NewPage();
            //创建证明材料
            CreateAttachment(ref doc, project);

            //创建附件目录
            CreateAccessory(ref doc, project.AccessoryList);

            //创建附件图片
            CreateImages(ref doc, project, inner_id);

            //doc.NewPage();
            //创建完成人确认表
            //CreateSort(doc);

            //关闭document
            doc.Close();

            List<string> fileList = new List<string>();
            fileList.Add(path1);
            if (!string.IsNullOrEmpty(contentPdfPath))
            {
                fileList.Add(contentPdfPath);
            }
            fileList.Add(path2);
            string errorMessage = string.Empty;
            MergePDFFiles(fileList.ToArray(), path, inner_id, ref  doc, ref errorMessage);

            //打开pdf,看效果
            try
            {
                Process.Start(path);
            }
            catch
            {
                MessageBox.Show("检测到有PDF阅读器正在运行，请关闭PDF阅读器后重新执行此操作。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return true;
        }
        #endregion

        private static string CreateContentPdf(string inner_id)
        {
            string path = System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "_" + Guid.NewGuid().ToString() + ".pdf";
            string tmpPdfId = Guid.NewGuid().ToString();
            string sql = string.Format("select {0} from [project] where inner_id='{1}'", "ContentImage1", inner_id);
            object obj = SqlHelper.ExecuteScalar(constr, CommandType.Text, sql);
            byte[] data = obj as byte[];
            if (data != null)
            {
                Common.CreatePdfFile(data, path);
                return path;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 加载生成PDF文档的基本信息，包括尺寸、字体等
        /// </summary>
        private static Document LoadBasic(string path)
        {
            //定义一个Document,并设置页面大小为A4
            Document doc = new Document(PageSize.A4, 30, 20, 25, 25);
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));
            //打开document
            doc.Open();
            doc.AddTitle("公安部科学技术奖推荐书（2012版）");
            return doc;
        }

        private static void LoadFont()
        {
            //载入字体
            BaseFont.AddToResourceSearch("iTextAsian.dll");
            BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
            BaseFont bfHei = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            BaseFont bfSong = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //BaseFont bfSong = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            fontHei = new Font(bfHei, 16);
            fontSong = new Font(bfSong, 12);
            fontSmallSong = new Font(bfSong, 10);
        }

        /// <summary>
        /// 加载生成PDF文档的基本信息，包括尺寸、字体等
        /// </summary>
        private static void LoadBasic(string inner_id, bool isNull, int pageIndex, string path, ref Document doc)
        {
            string errorMessage = string.Empty;
            if (isNull)
            {
                project = new Project();
            }
            else
            {
                project = GetProject(inner_id, ref errorMessage);
            }

            //定义一个Document,并设置页面大小为A4
            doc = new Document(PageSize.A4, 30, 20, 25, 25);

            //写实例
            string tempIndexName = pageIndex == -1 ? "" : pageIndex.ToString();

            CloseProcessByName("AcroRd32");

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));

            //打开document
            doc.Open();

            //载入字体
            LoadFont();
            //写入PDF文档基本信息
            doc.AddTitle("公安部科学技术奖推荐书（2012版）");
        }

        public static void CloseProcessByName(string name)
        {
            Process[] processArr = Process.GetProcessesByName(name);
            if (processArr.Length > 0)
            {
                foreach (var process in processArr)
                {
                    if (!process.HasExited)
                    {
                        process.Kill();
                    }
                }
            }
        }

        #region 预览当前页
        /// <summary>
        /// 预览当前页
        /// </summary>
        /// <param name="pageIndex"></param>
        public static void ViewCurrentPage(int index, string inner_id, bool isJpgOrPdf)
        {
            Document doc = null;
            string path = System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + index.ToString() + ".pdf";
            LoadBasic(inner_id, false, index, path, ref doc);

            switch (index)
            {
                case 0:
                    //创建项目基本情况
                    CreateProjectBasic(ref doc, project);
                    break;
                case 1:
                    //创建项目简介
                    CreateProjectIntro(ref doc, project);
                    break;
                case 2:
                    //创建主要技术创新点
                    CreateProjectInnovation(doc, project);
                    break;
                case 3:
                    //创建项目技术内容
                    if (isJpgOrPdf)
                    {
                        CreateProjectContent(ref doc, project);
                    }
                    else
                    {
                        string tempPath = CreateContentPdf(inner_id);
                        if (!string.IsNullOrEmpty(tempPath))
                        {
                            CloseProcessByName("AcroRd32");
                            try
                            {
                                Process.Start(tempPath);
                            }
                            catch
                            {
                                MessageBox.Show("检测到有PDF阅读器正在运行，请关闭PDF阅读器后重新执行此操作。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                        return;
                    }
                    //CreateContentImage(doc, project);
                    break;
                case 4:
                    //创建项目应用情况
                    CreateProjectAppIntro(ref doc, project);
                    break;
                case 5:
                    //创建社会、经济效益
                    CreateProjectSocialAndEconomic(ref doc, project);
                    break;
                case 6:
                    //创建主要完成人情况表
                    CreateAuthor(ref doc, project.AuthorList);
                    break;
                case 7:
                    //创建主要完成单位情况及意见表
                    CreateUnit(ref doc, project.UnitList);
                    break;
                case 8:
                    //创建证明材料
                    CreateAttachment(ref doc, project);
                    break;
                case 9:
                    //创建附件目录
                    CreateAccessory(ref doc, project.AccessoryList);
                    CreateImages(ref doc, project, inner_id);
                    break;
                default:
                    break;
            }

            //关闭document
            doc.Close();

            CloseProcessByName("AcroRd32");
            //打开pdf,看效果
            try
            {
                Process.Start(path);
            }
            catch
            {
                MessageBox.Show("检测到有PDF阅读器正在运行，请关闭PDF阅读器后重新执行此操作。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

        #region 创建项目基本情况
        /// <summary>
        /// 创建项目基本情况
        /// </summary>
        private static void CreateProjectBasic(ref Document doc, Project project)
        {
            Paragraph pgTitle = new Paragraph("公安部科学技术奖推荐书（2012版）", fontHei);

            pgTitle.Alignment = Element.ALIGN_CENTER;
            doc.Add(pgTitle);

            Paragraph pg = new Paragraph("一、项目基本情况", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 35;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(32);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("专业评审组名称", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(string.Empty), 15));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("项目总编号", fontSong), 6), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(string.Empty), 4));
            table.AddCell(PDFHelper.CreateCell(new Chunk("项目名称（限40个字，必须  填写可公布的项目名称或项目代号）", fontSong), 7));
            PdfPCell cellName = PDFHelper.CreateCell(new Chunk(project.Name, fontSong), 25);
            cellName.FixedHeight = 50;
            table.AddCell(cellName);
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("主要完成人", fontSong), 7), normalCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.MainAuthor, fontSong), 25));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("主要完成单位", fontSong), 7), normalCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.MainUnit, fontSong), 25));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("第一完成单位项目联系人（前5人之一）", fontSong), 7, 2), smallCellHeight * 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Unit1Contact, fontSong), 9, 2));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("电话", fontSong), 3), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Unit1Phone, fontSong), 13));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("手机", fontSong), 3), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Unit1Mobile, fontSong), 13));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("成果登记号", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.ResultRegId, fontSong), 12));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("推荐书密级", fontSong), 6), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.SecLevel, fontSong), 8));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("应用专业", fontSong), 7, 2), smallCellHeight * 2));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("一级", fontSong), 3), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Level1Major, fontSong), 22));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("二级", fontSong), 3), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Level2Major, fontSong), 22));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("技术领域", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.TechArea, fontSong), 25));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("奖励类型", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.AwardType, fontSong), 25));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("主 题 词", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Subject, fontSong), 25));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("项目来源", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.ProSource1, fontSong), 5));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("计划名称和编号", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.PlanNameId, fontSong), 13));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("计划投入(万元)", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.PlanTouru, fontSong), 12));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("匹配(万元)", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Pipei, fontSong), 6));
            //table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("计划名称和编号", fontSong)), smallCellHeight));
            //table.AddCell(PDFHelper.CreateCell(new Chunk(project.PlanNameId, fontSong), 3));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("自筹(万元)", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Prepare, fontSong), 12));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("其他投入(万元)", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.OtherTouru, fontSong), 6));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("项目开始时间", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.StartTime, fontSong), 12));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("项目完成时间", fontSong), 7), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.EndTime, fontSong), 6));
            doc.Add(table);
        }
        #endregion

        #region 创建项目简介
        /// <summary>
        /// 创建项目简介
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="project"></param>
        private static void CreateProjectIntro(ref Document doc, Project project)
        {
            Paragraph pg = new Paragraph("二、项目简介", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(2);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.CreateCell(new Paragraph("介绍项目的主要用途、功能，解决的主要技术难题，提升的关键技术指标，应用推广情况、应用效果及范围。限800字。对于涉密项目如不能公布项目简介，请在下面注明。", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("项目简介内容可否公布", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.IsPublish, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(project.Introduction, fontSong), 2), 550));
            doc.Add(table);
        }
        #endregion

        #region 创建主要技术创新点
        /// <summary>
        /// 创建主要技术创新点
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="project"></param>
        private static void CreateProjectInnovation(Document doc, Project project)
        {
            Paragraph pg = new Paragraph("三、主要技术创新点", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(1);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.CreateCell(new Paragraph("归纳和提炼项目中具有创造性的关键技术点。每个技术创新点应相对独立存在，按其重要程度排序，用序号分开。限800字。", fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(project.Innovation, fontSong)), 620));
            doc.Add(table);
        }
        #endregion

        #region 创建项目技术内容
        /// <summary>
        /// 创建项目技术内容
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="project"></param>
        private static void CreateProjectContent(ref Document doc, Project project)
        {
            Paragraph pg = new Paragraph("四、项目技术内容", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(1);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.CreateCell(new Paragraph("内容应包括：1、立项背景；2、技术内容；3、主要技术参数比较。限8页。", fontSong),
                new Paragraph("如未写满8页，请将空白页删除。", fontSong)));

            if (project.ContentType == 0)
            {
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph()), 480));
                doc.Add(table);

                CreateContentImages(ref doc, project);
            }
            else if (project.ContentType == 1)
            {
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph()), 480));
                doc.Add(table);
                //if (!string.IsNullOrEmpty(project.Content))
                //{
                //    table.AddCell(PDFHelper.CreateCell(new Chunk(project.Content, fontSong)));
                //}
                //else
                //{
                //    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph()), 480));
                //}

                ////创建项目技术内容图片
                //CreateContentImage(ref doc, project);
            }
        }
        #endregion

        private static void CreateContentImages(ref Document doc, Project project)
        {
            System.Drawing.Image[] imageArr = new System.Drawing.Image[] { project.ContentImage1, project.ContentImage2, project.ContentImage3, project.ContentImage4, project.ContentImage5, project.ContentImage6, project.ContentImage7, project.ContentImage8 };

            bool flag = false;  //是否横版
            if (imageArr == null || imageArr.Length == 0)
            {
                return;
            }

            int count = 1;      //第几张图片
            foreach (System.Drawing.Image contentImage in imageArr)
            {
                if (contentImage == null)
                {
                    continue;
                }

                System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)contentImage;
                Image image = null;
                using (MemoryStream ms = new MemoryStream())
                {

                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                    image = Image.GetInstance(ms.ToArray());
                }

                PdfPTable table = null;
                int max = 600;
                if (image.Width > image.Height && image.Width > max)
                {
                    if (image.Width > max)
                    {
                        bitmap = new helper().KiResizeImage(bitmap, max, (int)(image.Height * max / image.Width), 0);
                        using (MemoryStream stream = new MemoryStream())
                        {
                            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                            image = Image.GetInstance(stream.ToArray());
                        }
                    }
                    if (!flag)
                    {
                        doc.SetPageSize(PageSize.A4.Rotate());
                        flag = true;
                    }
                    doc.NewPage();
                    table = new PdfPTable(1);
                    table.SpacingBefore = 10;
                    table.AddCell(PDFHelper.CreateCell(new Chunk("第" + count.ToString() + "张图片")));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 5, 5, 5, 5), image.Height));
                }
                else
                {
                    if (image.Height > max)
                    {
                        bitmap = new helper().KiResizeImage(bitmap, (int)(image.Width * max / image.Height), max, 0);
                        using (MemoryStream stream = new MemoryStream())
                        {
                            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                            image = Image.GetInstance(stream.ToArray());
                        }
                    }
                    if (flag)
                    {
                        doc.SetPageSize(PageSize.A4);
                        flag = false;
                    }
                    doc.NewPage();
                    table = new PdfPTable(1);
                    table.SpacingBefore = 10;
                    table.AddCell(PDFHelper.CreateCell(new Chunk("第" + count.ToString() + "张图片")));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 5, 5, 5, 5), image.Height));

                }
                doc.Add(table);
                count++;
            }
            doc.SetPageSize(PageSize.A4);
        }

        #region 创建项目应用情况
        /// <summary>
        /// 创建项目应用情况
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="project"></param>
        private static void CreateProjectAppIntro(ref Document doc, Project project)
        {
            Paragraph pg = new Paragraph("五、项目应用情况", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(5);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.CreateCell(new Paragraph("应用概述：描述项目的应用、推广情况及预期应用前景。限500字。", fontSong), 5));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(project.AppIntro, fontSong), 5), 310));
            //table.AddCell(PDFHelper.CreateCell(new Paragraph("应用单位列表：", fontSong), 5));
            //table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(project.AppUnit, fontSong), 5), 310));
            table.AddCell(PDFHelper.CreateCell(new Chunk("应用单位列表：", fontSong), 5));
            table.AddCell(PDFHelper.CreateCell(new Chunk("应用单位名称", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("联系人", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("联系电话", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("应用时间", fontSong)));

            int appCount = 0;
            if (project != null && project.AppUnitList != null)
            {
                for (int i = 0; i < project.AppUnitList.Count; i++)
                {
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(project.AppUnitList[i].UnitName, fontSong), 2), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(project.AppUnitList[i].Contact, fontSong)), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(project.AppUnitList[i].Phone, fontSong)), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(project.AppUnitList[i].UnitTime, fontSong)), smallCellHeight));
                    appCount++;
                }
                for (; appCount < 11; appCount++)
                {
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong), 2), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)), smallCellHeight));
                }
            }
            else
            {
                for (int i = 0; i < 11; i++)
                {
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong), 2), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)), smallCellHeight));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)), smallCellHeight));
                }
            }
            doc.Add(table);
        }
        #endregion

        #region 创建社会、经济效益
        /// <summary>
        /// 创建社会、经济效益
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="project"></param>
        private static void CreateProjectSocialAndEconomic(ref Document doc, Project project)
        {
            Paragraph pg = new Paragraph("六、社会、经济效益", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(4);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.CreateCell(new Paragraph("社会效益：描述在促进公安业务的现代化建设，提高控制、防范和打击犯罪及维护社会稳定能力等方面所起的作用。限400字。", fontSong), 4));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(project.Social, fontSong), 4), 260));
            table.AddCell(PDFHelper.CreateCell(new Paragraph("经济效益：概述该项目在推荐前三年第一完成单位所取得的经济效益。并加盖财务章。", fontSong), 4));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(project.Economic, fontSong), 4), 260));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("财务联系人", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.Finance, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("电话", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(project.FinancePhone, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("财务单位章", fontSong), 4), normalCellHeight));
            //doc.Add(new Chunk("\n"))
            doc.Add(table);

        }
        #endregion

        #region 创建推荐意见
        /// <summary>
        /// 创建推荐意见
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="project"></param>
        private static void Create推荐意见(ref Document doc, RecommendOpinion ro)
        {
            if (ro.Name != "")
            {
                Paragraph pg = new Paragraph("推荐单位意见", fontHei);
                pg.Alignment = Element.ALIGN_CENTER;
                pg.SpacingBefore = 20;
                doc.Add(pg);

                PdfPTable table = new PdfPTable(7);
                table.SpacingBefore = tableBefore;
                table.AddCell(PDFHelper.CreateCell(new Paragraph("推荐单位确认推荐资料属实后，根据推荐项目的技术创新点、技术经济指标、促进行业科技进步、应用情况、完成人情况，并参照《公安部科学技术奖励办法》的授奖条件，写明推荐理由和建议等级，并加盖单位公章。不少于300字，控制在600字以内。", fontSmallSong), 7));
                table.AddCell(PDFHelper.CreateCell(new Chunk("项目名称", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(ro.Name, fontSong), 6));
                table.AddCell(PDFHelper.CreateCell(new Chunk("推荐等级", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(ro.Level, fontSong), 6));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(ro.Remark, fontSong), 7), 550));
                doc.Add(table);
            }
        }
        #endregion

        #region 创建附件目录
        /// <summary>
        /// 创建附件目录
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="project"></param>
        private static void Create附件目录(ref Document doc, Project p)
        {
            Paragraph pg = new Paragraph("十、附件目录", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(7);

            doc.Add(table);
        }
        #endregion

        private static void CreateSingleAuthor(ref Document doc, Author author)
        {
            PdfPTable table = new PdfPTable(6);
            doc.Add(new Chunk("\n"));
            table.SpacingBefore = tableBefore;

            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("姓    名", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Name, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("性别", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Sex, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("排名", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Order.ToString(), fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("出生日期", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Birthday, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("民族", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Nation, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("党派", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Party, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("工作单位", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Unit, fontSong), 5));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("现任职务", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.CurrentPosition, fontSong), 5));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("联系电话", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Phone, fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("移动电话", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Mobile, fontSong), 2));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("本科毕业学校", fontSmallSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.College, fontSong), 3));
            table.AddCell(PDFHelper.CreateCell(new Chunk("毕业时间", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.GraduateTime, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("最高学位", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.HighestDegree, fontSong), 3));
            table.AddCell(PDFHelper.CreateCell(new Chunk("获得时间", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.HighestDegreeTime, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("最高学位学校", fontSmallSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.HighestDegreeSchool, fontSong), 5));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("技术职称", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.Title, fontSong), 3));
            table.AddCell(PDFHelper.CreateCell(new Chunk("技术职称等级", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.TitleLevel, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("熟悉专业", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.FamiliarProfessional, fontSong), 5));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("曾获科技奖励", fontSong), 2), 60));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.WonAward, fontSong), 4));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("参加本项目的起止时间", fontSong), 2), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk("从", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.StartTime, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("至", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(author.EndTime, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph("在本项目的组织、研究、推广中做出的创造性贡献：", fontSong), 6), smallCellHeight));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(author.Contribution, fontSong), 6), 180));
            table.AddCell(PDFHelper.CreateCell(new Chunk("声明", new Font(BaseFont.CreateFont(@"C:\WINDOWS\FONTS\SIMHEI.TTF", BaseFont.IDENTITY_V, BaseFont.NOT_EMBEDDED), 16)), 0, 0, 15, 30));
            table.AddCell(PDFHelper.CreateCell(0, 0, 5, 3, 5, 1, new Paragraph("        本人对推荐书内容及全部附件材料进行了审查，全部内容和材料属实，并对推荐材料的真实性负责。", fontSong),
                                                new Paragraph("                                                                              本人签名： ______________              年      月        日 ", fontSong)));
            doc.Add(table);
        }

        #region 创建主要完成人情况表
        /// <summary>
        /// 创建主要完成人情况表
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="author"></param>
        private static void CreateAuthor(ref Document doc, List<Author> authorList)
        {
            Paragraph pg = new Paragraph("七、主要完成人情况表", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            if (authorList == null)
            {
                authorList = new List<Author>();
            }
            if (authorList.Count == 0)
            {
                authorList.Add(new Author());
            }

            for (int i = 0; i < authorList.Count; i++)
            {
                Author author = authorList[i];
                PdfPTable table = new PdfPTable(6);
                if (i > 0)
                {
                    doc.Add(new Chunk("\n"));
                }
                table.SpacingBefore = tableBefore;

                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("姓    名", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Name, fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk("性别", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Sex, fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk("排名", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Order.ToString(), fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("出生日期", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Birthday, fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk("民族", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Nation, fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk("党派", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Party, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("工作单位", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Unit, fontSong), 5));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("现任职务", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.CurrentPosition, fontSong), 5));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("联系电话", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Phone, fontSong), 2));
                table.AddCell(PDFHelper.CreateCell(new Chunk("移动电话", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Mobile, fontSong), 2));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("本科毕业学校", fontSmallSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.College, fontSong), 3));
                table.AddCell(PDFHelper.CreateCell(new Chunk("毕业时间", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.GraduateTime, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("最高学位", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.HighestDegree, fontSong), 3));
                table.AddCell(PDFHelper.CreateCell(new Chunk("获得时间", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.HighestDegreeTime, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("最高学位学校", fontSmallSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.HighestDegreeSchool, fontSong), 5));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("技术职称", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.Title, fontSong), 3));
                table.AddCell(PDFHelper.CreateCell(new Chunk("技术职称等级", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.TitleLevel, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("熟悉专业", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.FamiliarProfessional, fontSong), 5));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("曾获科技奖励", fontSong), 2), 60));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.WonAward, fontSong), 4));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("参加本项目的起止时间", fontSong), 2), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk("从", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.StartTime, fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk("至", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(author.EndTime, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph("在本项目的组织、研究、推广中做出的创造性贡献：", fontSong), 6), smallCellHeight));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(author.Contribution, fontSong), 6), 180));
                table.AddCell(PDFHelper.CreateCell(new Chunk("声明", new Font(BaseFont.CreateFont(@"C:\WINDOWS\FONTS\SIMHEI.TTF", BaseFont.IDENTITY_V, BaseFont.NOT_EMBEDDED), 16)), 0, 0, 15, 30));
                table.AddCell(PDFHelper.CreateCell(0, 0, 5, 3, 5, 1, new Paragraph("        本人对推荐书内容及全部附件材料进行了审查，全部内容和材料属实，并对推荐材料的真实性负责。", fontSong),
                                                    new Paragraph("                                                                              本人签名： ______________              年      月        日 ", fontSong)));
                //new Paragraph("                                                                                                      年      月        日", fontSong))
                //doc.Add(new Chunk("\n"));
                doc.Add(table);
                if (i < authorList.Count - 1)
                {
                    doc.NewPage();
                }
            }
        }
        #endregion

        private static void CreateSingleUnit(ref Document doc, Unit unit)
        {
            PdfPTable table = new PdfPTable(6);
            doc.Add(new Chunk("\n"));
            table.SpacingBefore = tableBefore;

            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("单位名称", fontSong)), normalCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Name, fontSong), 3));
            table.AddCell(PDFHelper.CreateCell(new Chunk("排名", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Order.ToString(), fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("所 在 地", fontSong)), normalCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Location, fontSong), 3));
            table.AddCell(PDFHelper.CreateCell(new Chunk("传    真", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Fax, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("联 系 人", fontSong)), smallCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Contact, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("联系电话", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Phone, fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("移动电话", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Mobile, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("通讯地址", fontSong)), normalCellHeight));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Address, fontSong), 3));
            table.AddCell(PDFHelper.CreateCell(new Chunk("邮政编码", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Zip, fontSong)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("对本项目研究、推广应用等的贡献：", fontSong), 6), smallCellHeight));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("（写明本单位在项目研制、开发、投产、应用和推广过程中提供技术、设备、人员和资金等条件，对项目的完成起到组织、管理和协调等作用。限500字。）\n" + unit.Contribution, fontSmallSong), 6), 360));
            table.AddCell(PDFHelper.CreateCell(0, 0, 15, 20, 1, 1, new Paragraph("审核", new Font(BaseFont.CreateFont(@"C:\WINDOWS\FONTS\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 16)), new Paragraph("意见", new Font(BaseFont.CreateFont(@"C:\WINDOWS\FONTS\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 16))));
            table.AddCell(PDFHelper.CreateCell(0, 0, 5, 3, 5, 1, new Paragraph("        本单位已将申报项目在单位内部进行了公示，未收到异议。经核实技术创新点、应用情况及上述贡献属实。", fontSong),
                                                new Paragraph("单位领导（签字）：                                             完成单位（公章）                                         ", fontSong),
                                                new Paragraph("                                                                                                                      年      月        日", fontSong)));
            doc.Add(table);
        }

        #region 创建主要完成单位情况及意见表
        /// <summary>
        /// 创建主要完成单位情况及意见表
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="unit"></param>
        private static void CreateUnit(ref Document doc, List<Unit> unitList)
        {
            Paragraph pg = new Paragraph("八、主要完成单位情况表", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            if (unitList == null)
            {
                unitList = new List<Unit>();
            }
            if (unitList.Count == 0)
            {
                unitList.Add(new Unit());
            }

            for (int i = 0; i < unitList.Count; i++)
            {
                Unit unit = unitList[i];

                PdfPTable table = new PdfPTable(6);
                if (i > 0)
                {
                    doc.Add(new Chunk("\n"));
                }
                table.SpacingBefore = tableBefore;

                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("单位名称", fontSong)), normalCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Name, fontSong), 3));
                table.AddCell(PDFHelper.CreateCell(new Chunk("排名", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Order.ToString(), fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("所 在 地", fontSong)), normalCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Location, fontSong), 3));
                table.AddCell(PDFHelper.CreateCell(new Chunk("传    真", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Fax, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("联 系 人", fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Contact, fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk("联系电话", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Phone, fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk("移动电话", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Mobile, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("通讯地址", fontSong)), normalCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Address, fontSong), 3));
                table.AddCell(PDFHelper.CreateCell(new Chunk("邮政编码", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(unit.Zip, fontSong)));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("对本项目研究、推广应用等的贡献：", fontSong), 6), smallCellHeight));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("（写明本单位在项目研制、开发、投产、应用和推广过程中提供技术、设备、人员和资金等条件，对项目的完成起到组织、管理和协调等作用。限500字。）\n" + unit.Contribution, fontSmallSong), 6), 360));
                table.AddCell(PDFHelper.CreateCell(0, 0, 15, 20, 1, 1, new Paragraph("审核", new Font(BaseFont.CreateFont(@"C:\WINDOWS\FONTS\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 16)), new Paragraph("意见", new Font(BaseFont.CreateFont(@"C:\WINDOWS\FONTS\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 16))));
                table.AddCell(PDFHelper.CreateCell(0, 0, 5, 3, 5, 1, new Paragraph("        本单位已将申报项目在单位内部进行了公示，未收到异议。经核实技术创新点、应用情况及上述贡献属实。", fontSong),
                                                    new Paragraph("单位领导（签字）：                                             完成单位（公章）                                         ", fontSong),
                                                    new Paragraph("                                                                                                                      年      月        日", fontSong)));
                doc.Add(table);
                if (i < unitList.Count - 1)
                {
                    doc.NewPage();
                }
            }
        }
        #endregion

        #region 创建附件目录
        /// <summary>
        /// 创建附件目录
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="accessoryList"></param>
        private static void CreateAccessory(ref Document doc, List<Accessory> accessoryList)
        {
            Paragraph pg = new Paragraph("十、附件目录", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(6);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("附件序号", fontSong)), smallCellHeight));
            Paragraph pgName = new Paragraph("附件名称", fontSong);
            pgName.Alignment = Element.ALIGN_CENTER;
            table.AddCell(PDFHelper.CreateCell(pgName, 5));

            if (accessoryList == null)
            {
                accessoryList = new List<Accessory>();
            }
            if (accessoryList.Count == 0)
            {
                accessoryList.Add(new Accessory());
            }

            foreach (Accessory accessory in accessoryList)
            {
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(accessory.Order.ToString(), fontSong)), smallCellHeight));
                table.AddCell(PDFHelper.CreateCell(new Chunk(accessory.Name, fontSong), 5));
            }
            doc.Add(table);
        }
        #endregion


        #region 创建空的证明材料
        /// <summary>
        /// 创建空的证明材料
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="accessoryList"></param>
        private static void CreateKZMCL(ref Document doc, Project project)
        {
            Paragraph pg = new Paragraph("九、证明材料", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);
            PdfPTable table = new PdfPTable(6);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("附件序号", fontSong)), smallCellHeight));
            Paragraph pgName = new Paragraph("附件名称", fontSong);
            pgName.Alignment = Element.ALIGN_CENTER;
            table.AddCell(PDFHelper.CreateCell(pgName, 5));
            doc.Add(table);
            doc.NewPage();
        }
        #endregion
        #region 创建完成人排序确认表
        /// <summary>
        /// 创建完成人排序确认表
        /// </summary>
        public static void CreateSort(string name, string inner_id)
        {
            Document doc = new Document(PageSize.A4, 30, 20, 25, 25);
            string path = System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + "完成人排序确认表.pdf";

            //写实例
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));
            string message = string.Empty;
            Project proj = GetProject(inner_id, ref message);

            //打开document
            doc.Open();

            //载入字体
            BaseFont.AddToResourceSearch("iTextAsian.dll");
            BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
            BaseFont bfHei = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            BaseFont bfSong = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontHei = new Font(bfHei, 16);
            fontSong = new Font(bfSong, 12);

            Paragraph pg = new Paragraph("完成人排序确认表", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 40;
            doc.Add(pg);

            doc.Add(new Paragraph("         项目名称：  " + name, fontSong));

            PdfPTable table = new PdfPTable(7);
            table.SpacingBefore = 10;
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("序号", fontSong)), 15));
            table.AddCell(PDFHelper.CreateCell(new Chunk("姓名", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("单位", fontSong), 3));
            table.AddCell(PDFHelper.CreateCell(new Chunk("职务", fontSong)));
            table.AddCell(PDFHelper.CreateCell(new Chunk("签字", fontSong)));
            for (int i = 0; i < 15; i++)
            {
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk((i + 1).ToString(), fontSong)), 22));
                if (i < proj.AuthorList.Count)
                {
                    table.AddCell(PDFHelper.CreateCell(new Chunk(proj.AuthorList[i].Name, fontSong)));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(proj.AuthorList[i].Unit, fontSong), 3));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(proj.AuthorList[i].CurrentPosition, fontSong)));
                }
                else
                {
                    table.AddCell(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong), 3));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)));
                }
                table.AddCell(PDFHelper.CreateCell(new Chunk(string.Empty, fontSong)));
            }
            BaseFont bfSongV = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simsun.ttc,0", BaseFont.IDENTITY_V, BaseFont.NOT_EMBEDDED);
            Font fontSongV = new Font(bfSongV, 12);
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(7, 0, 0, 26, 1, 1, new Paragraph("退", fontSong), new Paragraph("出", fontSong), new Paragraph("报", fontSong), new Paragraph("奖", fontSong), new Paragraph("人", fontSong), new Paragraph("员", fontSong), new Paragraph("承", fontSong), new Paragraph("诺", fontSong)), 170));
            table.AddCell(PDFHelper.CreateCell(0, 0, 0, 0, 6, 1, new Paragraph("本人已知晓该项目报奖及其完成人排序，同意退出。", fontHei), new Paragraph("（所有人签同张纸上，如需代替，需在得到完成人认可后注明***代***。）", fontSong), new Paragraph("签字：", fontHei)));
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("确认\n意见", fontSong), 15, 0, 0, 20), 80));
            table.AddCell(PDFHelper.CreateCell(0, 0, 0, 0, 6, 1, new Paragraph("上述完成人排序已经项目组全体成员讨论通过，签字属实。", fontHei), new Paragraph("                          （项目负责人签字： ______________ ）", fontSong)));
            doc.Add(table);

            doc.Close();
            CloseProcessByName("AcroRd32");
            try
            {
                Process.Start(path);
            }
            catch
            {
                MessageBox.Show("检测到有PDF阅读器正在运行，请关闭PDF阅读器后重新执行此操作。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

        private static void CreateSingleAttachment(ref Document doc, string title, System.Drawing.Image attImage, ref int count)
        {
            bool flag = false;  //是否横版
            System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)attImage;
            System.Drawing.Bitmap tempBmp = new System.Drawing.Bitmap((System.Drawing.Image)bitmap);
            Image image = null;
            using (MemoryStream stream = new MemoryStream())
            {
                tempBmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                image = Image.GetInstance(stream.ToArray());
            }
            if (bitmap.Width > bitmap.Height)
            {
                if (!flag)
                {
                    doc.SetPageSize(PageSize.A4.Rotate());
                    flag = true;
                }
                doc.NewPage();
                doc.Add(new Chunk("       " + (++count).ToString() + ". " + title + "\n", fontSong));
                doc.Add(new Chunk("\n"));
                image.ScaleToFit(doc.PageSize.Width - 40, doc.PageSize.Height - 40);
            }
            else
            {
                if (flag)
                {
                    doc.SetPageSize(PageSize.A4);
                    flag = false;
                }
                doc.NewPage();
                doc.Add(new Chunk("       " + (++count).ToString() + ". " + title + "\n", fontSong));
                doc.Add(new Chunk("\n"));
                image.ScaleToFit(doc.PageSize.Width - 50, doc.PageSize.Height - 100);
            }
            doc.Add(image);
        }

        #region 创建证明材料
        /// <summary>
        /// 创建证明材料
        /// </summary>
        /// <param name="doc"></param>
        private static void CreateAttachment(ref Document doc, Project project)
        {
            if (project == null)
            {
                return;
            }
            doc.NewPage();
            Paragraph pg = new Paragraph("九、证明材料", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(6);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("附件序号", fontSong)), smallCellHeight));
            Paragraph pgName = new Paragraph("附件名称", fontSong);
            pgName.Alignment = Element.ALIGN_CENTER;
            table.AddCell(PDFHelper.CreateCell(pgName, 5));

            List<string> titleList = null;
            List<System.Drawing.Image> imageList = null;

            GetAttachmentList(out titleList, out imageList, project);

            int count = 0;
            for (int i = 0; i < titleList.Count; i++)
            {
                if (imageList[i] != null)
                {
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk((++count).ToString(), fontSong)), smallCellHeight));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(titleList[i], fontSong), 5));
                }
            }
            doc.Add(table);
            doc.NewPage();
            count = 0;
            GC.Collect();
            bool flag = false;  //是否横版
            //double max = 560;
            for (int i = 0; i < titleList.Count; i++)
            {
                if (imageList[i] == null)
                {
                    continue;
                }
                System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)imageList[i];
                System.Drawing.Bitmap tempBmp = new System.Drawing.Bitmap((System.Drawing.Image)bitmap);
                //PdfPTable imageTable = null;
                Image image = null;
                using (MemoryStream stream = new MemoryStream())
                {
                    tempBmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    image = Image.GetInstance(stream.ToArray());
                    stream.Dispose();
                }
                tempBmp.Dispose();
                if (bitmap.Width > bitmap.Height)
                {
                    //横版图片
                    //if (bitmap.Height > max)
                    //{
                    //    //需要压缩
                    //    //bitmap = Common.ZipImage(bitmap, max / bitmap.Height, "tmp");

                    //    string newPath = System.Windows.Forms.Application.StartupPath + "\\" + "tmp" + "\\" + Guid.NewGuid().ToString() + ".jpg";
                    //    Common.GetPicThumbnail((System.Drawing.Image)bitmap, newPath, (int)(max), (int)(bitmap.Width * max / bitmap.Height), 100);
                    //    bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(newPath);
                    //    bitmap.Save("G:\\生成pdf压缩后" + i.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    //    //bitmap = new helper().KiResizeImage(bitmap, (int)max, (int)(bitmap.Height * max / bitmap.Width), 0);
                    //}
                    //using (MemoryStream stream = new MemoryStream())
                    //{
                    //    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    //    image = Image.GetInstance(stream.ToArray());
                    //}
                    if (!flag)
                    {
                        doc.SetPageSize(PageSize.A4.Rotate());
                        flag = true;
                    }
                    //imageTable = new PdfPTable(new float[] { (float)max + 10 });
                    //imageTable.SpacingBefore = 5;
                    //imageTable.AddCell(PDFHelper.CreateCell(new Chunk((i + 1).ToString() + ". " + titleList[i], fontSong)));
                    //imageTable.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 1, 1, 1, 1), image.Height));
                }
                else
                {
                    //竖版图片
                    //if (bitmap.Width > max)
                    //{
                    //    //需要压缩
                    //    //bitmap = Common.ZipImage(bitmap, max / bitmap.Width, "tmp");
                    //    string newPath = System.Windows.Forms.Application.StartupPath + "\\" + "tmp" + "\\" + Guid.NewGuid().ToString() + ".jpg";
                    //    Common.GetPicThumbnail((System.Drawing.Image)bitmap, newPath, (int)(bitmap.Height * max / bitmap.Width), (int)(max), 100);
                    //    bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(newPath);
                    //    bitmap.Save("G:\\生成pdf压缩后" + i.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    //    //bitmap = new helper().KiResizeImage(bitmap, (int)(bitmap.Width * max / bitmap.Height), (int)max, 0);
                    //}
                    //using (MemoryStream stream = new MemoryStream())
                    //{
                    //    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                    //    image = Image.GetInstance(stream.ToArray());
                    //}
                    if (flag)
                    {
                        doc.SetPageSize(PageSize.A4);
                        flag = false;
                    }
                    //imageTable = new PdfPTable(new float[] { (float)max + 10 });
                    //imageTable.SpacingBefore = 5;
                    //imageTable.AddCell(PDFHelper.CreateCell(new Chunk((i + 1).ToString() + ". " + titleList[i], fontSong)));
                    //imageTable.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 1, 1, 1, 1), image.Height));
                }
                doc.NewPage();
                doc.Add(new Chunk("       " + (++count).ToString() + ". " + titleList[i] + "\n", fontSong));
                doc.Add(new Chunk("\n"));
                image.ScaleToFit(doc.PageSize.Width - 60, doc.PageSize.Height - 110);
                doc.Add(image);
                
                image = null;
                //doc.Add(imageTable);
            }
            doc.SetPageSize(PageSize.A4);
        }
        #endregion

        #region 创建项目技术内容图片
        /// <summary>
        /// 创建项目技术内容图片
        /// </summary>
        /// <param name="doc"></param>
        private static void CreateContentImage(ref Document doc, Project project)
        {
            bool flag = false;  //是否横版
            if (project == null || project.ContentImageList == null || project.ContentImageList.Count == 0)
            {
                return;
            }
            foreach (ContentImage ci in project.ContentImageList)
            {
                if (ci == null || ci.ImageUrl == null)
                {
                    continue;
                }

                System.Drawing.Bitmap bitmap = ci.ImageUrl;
                Image image = null;
                using (MemoryStream ms = new MemoryStream())
                {

                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                    image = Image.GetInstance(ms.ToArray());
                }

                PdfPTable table = null;
                int max = 600;
                if (image.Width > image.Height && image.Width > max)
                {
                    if (image.Width > max)
                    {
                        bitmap = new helper().KiResizeImage(bitmap, max, (int)(image.Height * max / image.Width), 0);
                        using (MemoryStream stream = new MemoryStream())
                        {
                            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                            image = Image.GetInstance(stream.ToArray());
                        }
                    }
                    if (!flag)
                    {
                        doc.SetPageSize(PageSize.A4.Rotate());
                        flag = true;
                    }
                    doc.NewPage();
                    table = new PdfPTable(1);
                    table.SpacingBefore = 10;
                    table.AddCell(PDFHelper.CreateCell(new Chunk("附件" + ci.OrderNumber + ". " + ci.ImageName, fontSong)));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 5, 5, 5, 5), image.Height));
                }
                else
                {
                    if (image.Height > max)
                    {
                        bitmap = new helper().KiResizeImage(bitmap, (int)(image.Width * max / image.Height), max, 0);
                        using (MemoryStream stream = new MemoryStream())
                        {
                            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                            image = Image.GetInstance(stream.ToArray());
                        }
                    }
                    if (flag)
                    {
                        doc.SetPageSize(PageSize.A4);
                        flag = false;
                    }
                    doc.NewPage();
                    table = new PdfPTable(1);
                    table.SpacingBefore = 10;
                    table.AddCell(PDFHelper.CreateCell(new Chunk("附件" + ci.OrderNumber + ". " + ci.ImageName, fontSong)));
                    table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 5, 5, 5, 5), image.Height));

                }
                doc.Add(table);
            }
            doc.SetPageSize(PageSize.A4);
        }
        #endregion

        #region 创建附件图片
        /// <summary>
        /// 创建附件图片
        /// </summary>
        /// <param name="doc"></param>
        private static void CreateImages(ref Document doc, Project project, string inner_id)
        {
            //Paragraph pg = new Paragraph("完成人排序确认表", fontHei);
            //pg.Alignment = Element.ALIGN_CENTER;
            //pg.SpacingBefore = 40;
            //doc.Add(pg);
            //doc.Add(new Chunk("\n"));

            bool flag = false;  //是否横版
            if (project == null || project.AccessoryList == null || project.AccessoryList.Count == 0)
            {
                return;
            }

            string errorMessage = string.Empty;
            foreach (Accessory accessory in project.AccessoryList)
            {
                //if (accessory == null || accessory.Image == null)
                //{
                //    continue;
                //}

                //if (!File.Exists(accessory.Path))
                //{
                //    continue;
                //}
                //Image image = Image.GetInstance(accessory.Path);
                GC.Collect();
                Image image = null;
                //System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)accessory.Image;
                System.Drawing.Bitmap bitmap = GetAccessoryImage(inner_id, accessory.Order.ToString(), ref errorMessage);
                System.Drawing.Bitmap tempBmp = new System.Drawing.Bitmap((System.Drawing.Image)bitmap);
                //System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(accessory.Path);
                using (MemoryStream ms = new MemoryStream())
                {
                    tempBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    image = Image.GetInstance(ms.ToArray());

                }
                //PdfPTable table = null;
                if (image.Width > image.Height)
                {
                    //if (image.Width > max)
                    //{
                    //    bitmap = new helper().KiResizeImage(bitmap, max, (int)(image.Height * max / image.Width), 0);
                    //    using (MemoryStream stream = new MemoryStream())
                    //    {
                    //        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    //        image = Image.GetInstance(stream.ToArray());
                    //    }
                    //}
                    if (!flag)
                    {
                        doc.SetPageSize(PageSize.A4.Rotate());
                        flag = true;
                    }
                    //doc.NewPage();
                    //table = new PdfPTable(1);
                    //table.SpacingBefore = 10;
                    //table.AddCell(PDFHelper.CreateCell(new Chunk(accessory.Order.ToString() + ". " + accessory.Name)));
                    //table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 5, 5, 5, 5), image.Height));
                }
                else
                {
                    //if (image.Height > max)
                    //{
                    //    bitmap = new helper().KiResizeImage(bitmap, (int)(image.Width * max / image.Height), max, 0);
                    //    using (MemoryStream stream = new MemoryStream())
                    //    {
                    //        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                    //        image = Image.GetInstance(stream.ToArray());
                    //    }
                    //}
                    if (flag)
                    {
                        doc.SetPageSize(PageSize.A4);
                        flag = false;
                    }
                    //doc.NewPage();
                    //table = new PdfPTable(1);
                    //table.SpacingBefore = 10;
                    //table.AddCell(PDFHelper.CreateCell(new Chunk(accessory.Order.ToString() + ". " + accessory.Name)));
                    //table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(image, 5, 5, 5, 5), image.Height));
                }
                doc.NewPage();
                doc.Add(new Chunk("    " + accessory.Order.ToString() + ". " + accessory.Name, fontSong));
                doc.Add(new Chunk("\n"));
                image.ScaleToFit(doc.PageSize.Width - 60, doc.PageSize.Height - 110);
                doc.Add(image);
                tempBmp.Dispose();
                bitmap.Dispose();
                //doc.Add(table);
            }
            doc.SetPageSize(PageSize.A4);
        }
        #endregion

        #region 获取输出PDF的项目实体信息
        /// <summary>
        /// 获取输出PDF的项目实体信息
        /// </summary>
        /// <param name="inner_id"></param>
        /// <returns></returns>
        public static Project GetProject(string inner_id, ref string errorMessage)
        {
            Project project = new Project();

            //加载项目基本信息
            string projectSql = "select [name], main_author, main_unit, unit1_contact, unit1_phone, unit1_mobile, result_reg_id, level1_major, level2_major, " +
                "tech_area, award_type, [subject], [pro_source], [pro_source1], plan_name_id,[Pipei], [PlanTouru], sec_level, start_time, end_time, " +
                "[IsPublish], Introduction, Innovation, [Content], AppIntro, AppUnit, Social, Economic, Finance, FinancePhone, [Prepare], " +
                "ChartTitle1, ChartPath1, ChartTitle2, ChartPath2, otherTouru, ContentImage1, ContentImage2, ContentImage3, ContentImage4, ContentImage5, ContentImage6, ContentImage7, ContentImage8, ContentType " +
                "from [project] where inner_id = '" + inner_id + "'";

            OleDbDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, projectSql);
                if (reader.HasRows && reader.Read())
                {
                    project.Name = reader["name"].ToString();
                    project.MainAuthor = reader["main_author"].ToString();
                    project.MainUnit = reader["main_unit"].ToString();
                    project.Unit1Contact = reader["unit1_contact"].ToString();
                    project.Unit1Phone = reader["unit1_phone"].ToString();
                    project.Unit1Mobile = reader["unit1_mobile"].ToString();
                    project.ResultRegId = reader["result_reg_id"].ToString();
                    project.Level1Major = reader["level1_major"].ToString();
                    project.Level2Major = reader["level2_major"].ToString();
                    project.TechArea = reader["tech_area"].ToString();
                    project.AwardType = reader["award_type"].ToString();
                    project.Subject = reader["subject"].ToString();
                    project.ProSource = reader["pro_source"].ToString();
                    project.ProSource1 = reader["pro_source1"].ToString();
                    project.PlanNameId = reader["plan_name_id"].ToString();
                    project.PlanTouru = reader["PlanTouru"].ToString();
                    project.Pipei = reader["Pipei"].ToString();
                    //project.Touru2 = reader["touru2"].ToString();
                    project.OtherTouru = reader["otherTouru"].ToString();
                    //project.PlanNameId = reader["plan_name_id"].ToString();
                    //project.ProYear = reader["pro_year"].ToString();
                    project.SecLevel = reader["sec_level"].ToString();
                    project.StartTime = reader["start_time"].ToString();
                    project.EndTime = reader["end_time"].ToString();
                    project.IsPublish = reader["IsPublish"].ToString();
                    project.Introduction = reader["Introduction"].ToString();
                    project.Innovation = reader["Innovation"].ToString();
                    project.Content = reader["Content"].ToString();
                    project.AppIntro = reader["AppIntro"].ToString();
                    project.AppUnit = reader["AppUnit"].ToString();
                    project.Social = reader["Social"].ToString();
                    project.Economic = reader["Economic"].ToString();
                    project.Finance = reader["Finance"].ToString();
                    project.FinancePhone = reader["FinancePhone"].ToString();
                    project.Prepare = reader["prepare"].ToString();
                    int contentType = 0;
                    int.TryParse(reader["ContentType"].ToString(), out contentType);
                    project.ContentType = contentType;

                    if (contentType == 0)
                    {
                        if (reader["ContentImage1"] != null && !reader.IsDBNull(35))
                        {
                            byte[] temp = (byte[])reader["ContentImage1"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage1 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }

                        if (reader["ContentImage2"] != null && !reader.IsDBNull(36))
                        {
                            byte[] temp = (byte[])reader["ContentImage2"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage2 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }

                        if (reader["ContentImage3"] != null && !reader.IsDBNull(37))
                        {
                            byte[] temp = (byte[])reader["ContentImage3"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage3 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }

                        if (reader["ContentImage4"] != null && !reader.IsDBNull(38))
                        {
                            byte[] temp = (byte[])reader["ContentImage4"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage4 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }

                        if (reader["ContentImage5"] != null && !reader.IsDBNull(39))
                        {
                            byte[] temp = (byte[])reader["ContentImage5"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage5 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }

                        if (reader["ContentImage6"] != null && !reader.IsDBNull(40))
                        {
                            byte[] temp = (byte[])reader["ContentImage6"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage6 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }

                        if (reader["ContentImage7"] != null && !reader.IsDBNull(41))
                        {
                            byte[] temp = (byte[])reader["ContentImage7"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage7 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }

                        if (reader["ContentImage8"] != null && !reader.IsDBNull(42))
                        {
                            byte[] temp = (byte[])reader["ContentImage8"];
                            using (MemoryStream ms = new MemoryStream(temp, 0, temp.Length))
                            {
                                project.ContentImage8 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                            }
                        }
                    }
                    //else if (contentType == 1)
                    //{
                    //if (reader["ContentImage1"] != null && !reader.IsDBNull(35))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage1"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf1.pdf");
                    //}
                    //if (reader["ContentImage2"] != null && !reader.IsDBNull(36))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage2"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf2.pdf");
                    //}
                    //if (reader["ContentImage3"] != null && !reader.IsDBNull(37))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage3"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf3.pdf");
                    //}
                    //if (reader["ContentImage4"] != null && !reader.IsDBNull(38))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage4"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf4.pdf");
                    //}
                    //if (reader["ContentImage5"] != null && !reader.IsDBNull(39))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage5"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf5.pdf");
                    //}
                    //if (reader["ContentImage6"] != null && !reader.IsDBNull(40))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage6"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf6.pdf");
                    //}
                    //if (reader["ContentImage7"] != null && !reader.IsDBNull(41))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage7"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf7.pdf");
                    //}
                    //if (reader["ContentImage8"] != null && !reader.IsDBNull(42))
                    //{
                    //    byte[] temp = (byte[])reader["ContentImage8"];
                    //    Common.CreatePdfFile(temp, System.Windows.Forms.Application.StartupPath + "\\" + inner_id + "\\" + inner_id + "pdf8.pdf");
                    //}
                    //}

                }
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            //加载主要完成人信息并排序
            LoadAuthorList(project, inner_id, ref errorMessage);

            //加载主要完成单位信息并排序
            LoadUnitList(project, inner_id, ref errorMessage);

            //加载附件信息并排序
            LoadAccessoryList(project, inner_id, ref errorMessage);

            //加载证明材料
            LoadAttachment(project, inner_id, ref errorMessage);

            //加载项目技术内容的图片
            LoadContentImage(project, inner_id, ref errorMessage);

            //加载应用单位列表
            LoadAppUnitList(project, inner_id, ref errorMessage);

            return project;
        }
        #endregion

        #region 加载应用单位列表
        /// <summary>
        /// 加载应用单位列表
        /// </summary>
        /// <param name="project"></param>
        /// <param name="inner_id"></param>
        /// <param name="errorMessage"></param>
        private static void LoadAppUnitList(Project project, string inner_id, ref string errorMessage)
        {
            string appUnitSql = "select Contact, UnitName, Phone, UnitTime from AppUnit where p_inner_id = '" + inner_id + "' order by [Order] asc";
            project.AppUnitList = new List<AppUnit>();

            OleDbDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, appUnitSql);
                while (reader.Read())
                {
                    AppUnit appUnit = new AppUnit();
                    appUnit.UnitName = reader["UnitName"].ToString();
                    appUnit.UnitTime = reader["UnitTime"].ToString();
                    appUnit.Phone = reader["Phone"].ToString();
                    appUnit.Contact = reader["Contact"].ToString();
                    project.AppUnitList.Add(appUnit);
                }
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        #endregion

        #region 加载主要完成人信息并排序
        /// <summary>
        /// 加载主要完成人信息并排序
        /// </summary>
        /// <param name="project"></param>
        /// <param name="inner_id"></param>
        private static void LoadAuthorList(Project project, string inner_id, ref string errorMessage)
        {
            string authorSql = "select name, sex, order, birthday, nation, party, unit, current_position, phone, mobile, college, graduate_time, highest_degree, " +
                "highest_degree_time, highest_degree_school, title, title_level, familiar_professional, won_award, start_time, end_time, contribution from author " +
                "where p_inner_id = '" + inner_id + "'";

            project.AuthorList = new List<Author>();

            OleDbDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, authorSql);
                while (reader.Read())
                {
                    Author author = new Author();
                    author.Name = reader["name"].ToString();
                    author.Sex = reader["sex"].ToString();
                    int order = 0;
                    author.Order = int.TryParse(reader["order"].ToString(), out order) ? order : 0;
                    author.Birthday = reader["birthday"].ToString();
                    author.Nation = reader["nation"].ToString();
                    author.Party = reader["party"].ToString();
                    author.Unit = reader["unit"].ToString();
                    author.CurrentPosition = reader["current_position"].ToString();
                    author.Phone = reader["phone"].ToString();
                    author.Mobile = reader["mobile"].ToString();
                    author.College = reader["college"].ToString();
                    author.GraduateTime = reader["graduate_time"].ToString();
                    author.HighestDegree = reader["highest_degree"].ToString();
                    author.HighestDegreeTime = reader["highest_degree_time"].ToString();
                    author.HighestDegreeSchool = reader["highest_degree_school"].ToString();
                    author.Title = reader["title"].ToString();
                    author.TitleLevel = reader["title_level"].ToString();
                    author.FamiliarProfessional = reader["familiar_professional"].ToString();
                    author.WonAward = reader["won_award"].ToString();
                    author.StartTime = reader["start_time"].ToString();
                    author.EndTime = reader["end_time"].ToString();
                    author.Contribution = reader["contribution"].ToString();
                    project.AuthorList.Add(author);
                }
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            project.AuthorList.Sort();
        }
        #endregion

        #region 加载主要完成单位信息并排序
        /// <summary>
        /// 加载主要完成单位信息并排序
        /// </summary>
        /// <param name="project"></param>
        /// <param name="inner_id"></param>
        private static void LoadUnitList(Project project, string inner_id, ref string errorMessage)
        {
            string unitSql = "select name, order, location, fax, contact, phone, mobile, address, zip, contribution from unit where p_inner_id = '" + inner_id + "'";

            project.UnitList = new List<Unit>();

            OleDbDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, unitSql);
                while (reader.Read())
                {
                    Unit unit = new Unit();
                    unit.Name = reader["name"].ToString();
                    int order = 0;
                    unit.Order = int.TryParse(reader["order"].ToString(), out order) ? order : 0;
                    unit.Location = reader["location"].ToString();
                    unit.Fax = reader["fax"].ToString();
                    unit.Contact = reader["contact"].ToString();
                    unit.Phone = reader["phone"].ToString();
                    unit.Mobile = reader["mobile"].ToString();
                    unit.Address = reader["address"].ToString();
                    unit.Zip = reader["zip"].ToString();
                    unit.Contribution = reader["contribution"].ToString();
                    project.UnitList.Add(unit);
                }
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            project.UnitList.Sort();
        }
        #endregion

        #region 加载证明材料
        /// <summary>
        /// 加载证明材料
        /// </summary>
        /// <param name="project"></param>
        /// <param name="inner_id"></param>
        private static void LoadAttachment(Project project, string inner_id, ref string errorMessage)
        {
            string sql = "select Type, File1, File2, File3, File4, File5, File6, File7, File8, File9 from Attachment where ProjectId = '" + inner_id + "'";

            OleDbDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, sql);
                if (reader.Read())
                {
                    Attachment attachment = new Attachment();
                    attachment.Type = reader["Type"].ToString();

                    if (!reader.IsDBNull(1))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File1"]))
                        {
                            attachment.File1 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }

                    if (!reader.IsDBNull(2))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File2"]))
                        {
                            attachment.File2 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    if (!reader.IsDBNull(3))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File3"]))
                        {
                            attachment.File3 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    if (!reader.IsDBNull(4))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File4"]))
                        {
                            attachment.File4 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    if (!reader.IsDBNull(5))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File5"]))
                        {
                            attachment.File5 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    if (!reader.IsDBNull(6))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File6"]))
                        {
                            attachment.File6 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    if (!reader.IsDBNull(7))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File7"]))
                        {
                            attachment.File7 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    if (!reader.IsDBNull(8))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File8"]))
                        {
                            attachment.File8 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    if (!reader.IsDBNull(9))
                    {
                        using (MemoryStream ms = new MemoryStream((byte[])reader["File9"]))
                        {
                            attachment.File9 = (System.Drawing.Image)System.Drawing.Bitmap.FromStream(ms);
                        }
                    }
                    project.Attachment = attachment;
                }
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        #endregion

        #region 加载项目技术内容的图片
        /// <summary>
        /// 加载项目技术内容的图片
        /// </summary>
        /// <param name="project"></param>
        /// <param name="inner_id"></param>
        private static void LoadContentImage(Project project, string inner_id, ref string errorMessage)
        {
            string sql = "select ImageName, ImageUrl, OrderNumber from content_image where ProjectId = '" + inner_id + "' order by OrderNumber asc";

            OleDbDataReader reader = null;
            List<ContentImage> list = new List<ContentImage>();
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, sql);
                while (reader.Read())
                {
                    ContentImage ci = new ContentImage();
                    ci.ImageName = reader["ImageName"].ToString();
                    ci.ImageUrl = ImageUtil.ConvertByteArrToBitmap((byte[])reader["ImageUrl"]);
                    ci.OrderNumber = reader["OrderNumber"].ToString();
                    list.Add(ci);
                }
                project.ContentImageList = list;
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        #endregion

        #region 获取单个附件的图片
        /// <summary>
        /// 获取单个附件的图片
        /// </summary>
        /// <param name="inner_id"></param>
        /// <param name="order"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        private static System.Drawing.Bitmap GetAccessoryImage(string inner_id, string order, ref string errorMessage)
        {
            string sql = "select image, [order] from accessory where p_inner_id = '" + inner_id + "' and [order] = '" + order.PadLeft(2, '0') + "'";
            System.Drawing.Bitmap bitmap = null;
            OleDbDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, sql);
                while (reader.Read())
                {
                    int tempOrder = 0;
                    int.TryParse(reader["order"].ToString(), out tempOrder);
                    using (MemoryStream ms = new MemoryStream((byte[])reader["image"]))
                    {
                        bitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(ms);
                    }
                }
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return bitmap;
        }
        #endregion

        #region 加载附件信息并排序
        /// <summary>
        /// 加载附件信息并排序
        /// </summary>
        /// <param name="project"></param>
        /// <param name="inner_id"></param>
        private static void LoadAccessoryList(Project project, string inner_id, ref string errorMessage)
        {
            string accessorySql = "select name, order, image, path from accessory where p_inner_id = '" + inner_id + "'";

            project.AccessoryList = new List<Accessory>();

            OleDbDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(constr, CommandType.Text, accessorySql);
                while (reader.Read())
                {
                    Accessory accessory = new Accessory();
                    accessory.Name = reader["name"].ToString();
                    int order = 0;
                    accessory.Order = int.TryParse(reader["order"].ToString(), out order) ? order : 0;
                    accessory.Path = reader["path"].ToString();
                    //using (MemoryStream ms = new MemoryStream((byte[])reader["image"]))
                    //{
                    //    accessory.Image = System.Drawing.Image.FromStream(ms);
                    //}
                    project.AccessoryList.Add(accessory);
                }
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            project.AccessoryList.Sort();
        }
        #endregion

        #region 从html编辑器的字符串中获取项目技术内容图片
        /// <summary>
        /// 从html编辑器的字符串中获取项目技术内容图片
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static Dictionary<string, string> GetImageDict(string str)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            string begin = "<IMG";
            string end = "\">";
            string pattern = "(?<=(" + begin + "))[.\\s\\S]*?(?=(" + end + "))";
            try
            {
                MatchCollection collection = Regex.Matches(str, pattern, RegexOptions.IgnoreCase);
                foreach (Match match in collection)
                {
                    string temp = GetInnerString(match.Value + "@@", "src=\"", "@@");
                    string imgHtml = begin + match.Value + end;
                    dict[imgHtml] = temp;
                }
            }
            catch
            {
                return dict;
            }
            return dict;
        }
        #endregion

        #region 打印推荐意见
        /// <summary>
        /// 打印推荐意见
        /// </summary>
        /// <param name="recommendOpinion"></param>
        public static void CreateRecommendOpinion(List<RecommendOpinion> list)
        {
            string path = System.Windows.Forms.Application.StartupPath + "\\tmp\\" + Guid.NewGuid().ToString() + ".pdf";
            Document doc = new Document();
            LoadPdfBasic(path, ref doc);

            foreach (var item in list)
            {
                doc.NewPage();
                Paragraph pg = new Paragraph("推荐单位意见", fontHei);
                pg.Alignment = Element.ALIGN_CENTER;
                pg.SpacingBefore = 20;
                doc.Add(pg);

                PdfPTable table = new PdfPTable(7);
                table.SpacingBefore = tableBefore;
                table.AddCell(PDFHelper.CreateCell(new Paragraph("推荐单位确认推荐资料属实后，根据推荐项目的技术创新点、技术经济指标、促进行业科技进步、应用情况、完成人情况，并参照《公安部科学技术奖励办法》的授奖条件，写明推荐理由和建议等级，并加盖单位公章。不少于300字，控制在600字以内。", fontSmallSong), 7));
                table.AddCell(PDFHelper.CreateCell(new Chunk("项目名称", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(item.Name, fontSong), 6));
                table.AddCell(PDFHelper.CreateCell(new Chunk("推荐等级", fontSong)));
                table.AddCell(PDFHelper.CreateCell(new Chunk(item.Level, fontSong), 6));
                table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Paragraph(item.Remark, fontSong), 7), 550));
                doc.Add(table);
            }

            doc.Close();

            CloseProcessByName("AcroRd32");
            try
            {
                Process.Start(path);
            }
            catch
            {
                MessageBox.Show("检测到有PDF阅读器正在运行，请关闭PDF阅读器后重新执行此操作。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

        public static void CreateAuditProject(List<AuditProject> list)
        {
            string path = System.Windows.Forms.Application.StartupPath + "\\tmp\\" + Guid.NewGuid().ToString() + ".pdf";
            Document doc = new Document();
            LoadPdfBasicH(path, ref doc);

            doc.NewPage();

            Paragraph pg = new Paragraph(DateTime.Now.Year.ToString() + "年度公安部科学技术奖推荐项目审核汇总表", fontHei);
            pg.Alignment = Element.ALIGN_CENTER;
            pg.SpacingBefore = 20;
            doc.Add(pg);

            PdfPTable table = new PdfPTable(17);
            table.SpacingBefore = tableBefore;
            table.AddCell(PDFHelper.CreateCell(new Chunk("序号", fontSong), 1));
            table.AddCell(PDFHelper.CreateCell(new Chunk("项目名称", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("完成单位", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("完成人", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("项目联系人", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("联系电话", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("手机", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("成果登记号", fontSong), 2));
            table.AddCell(PDFHelper.CreateCell(new Chunk("推荐等级", fontSong), 2));

            foreach (var item in list)
            {
                if (item.Level != "不授奖" && item.Level != "")
                {
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Order.ToString(), fontSong), 1));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Name, fontSong), 2));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Unit, fontSong), 2));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Author, fontSong), 2));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Contact, fontSong), 2));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Phone, fontSong), 2));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Mobile, fontSong), 2));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.AchievementId, fontSong), 2));
                    table.AddCell(PDFHelper.CreateCell(new Chunk(item.Level, fontSong), 2));
                }
            }
            doc.Add(table);

            doc.Close();

            CloseProcessByName("AcroRd32");
            try
            {
                Process.Start(path);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message , "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #region 加载PDF文件基本信息
        /// <summary>
        /// 加载PDF文件基本信息
        /// </summary>
        /// <param name="path"></param>
        private static void LoadPdfBasic(string path, ref Document doc)
        {
            //定义一个Document,并设置页面大小为A4
            doc = new Document(PageSize.A4, 30, 20, 25, 25);

            CloseProcessByName("AcroRd32");

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));

            //打开document
            doc.Open();

            //载入字体
            BaseFont.AddToResourceSearch("iTextAsian.dll");
            BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
            BaseFont bfHei = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            BaseFont bfSong = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //BaseFont bfSong = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            fontHei = new Font(bfHei, 16);
            fontSong = new Font(bfSong, 12);
            fontSmallSong = new Font(bfSong, 10);
        }
        #endregion

        #region 加载PDF文件基本信息
        /// <summary>
        /// 加载PDF文件基本信息
        /// </summary>
        /// <param name="path"></param>
        private static void LoadPdfBasicH(string path, ref Document doc)
        {
            //定义一个Document,并设置页面大小为A4横向
            Rectangle A4H = new Rectangle(PageSize.A4.Height, PageSize.A4.Width);
            doc = new Document(A4H, 30, 20, 25, 25);

            CloseProcessByName("AcroRd32");

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));

            //打开document
            doc.Open();

            //载入字体
            BaseFont.AddToResourceSearch("iTextAsian.dll");
            BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
            BaseFont bfHei = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            BaseFont bfSong = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //BaseFont bfSong = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            fontHei = new Font(bfHei, 16);
            fontSong = new Font(bfSong, 12);
            fontSmallSong = new Font(bfSong, 10);
        }
        #endregion
        #region 合并pdf文档
        /// <summary>  
        /// 合并pdf文档  
        /// </summary>  
        /// <param name="fileList"></param>  
        /// <param name="outMergeFile"></param>  
        private static void MergePDFFiles(string[] fileList, string outMergeFile, string inner_id, ref Document doc, ref string errorMessage)
        {
            try
            {
                PdfReader reader;
                doc = new Document();
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(outMergeFile, FileMode.Create));
                doc.Open();
                //LoadBasic(inner_id, false, -1, outMergeFile, ref doc);
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage newPage;
                for (int i = 0; i < fileList.Length; i++)
                {
                    reader = new PdfReader(fileList[i]);
                    int iPageNum = reader.NumberOfPages;
                    for (int j = 1; j <= iPageNum; j++)
                    {
                        doc.NewPage();
                        newPage = writer.GetImportedPage(reader, j);
                        cb.AddTemplate(newPage, 0, 0);
                    }
                }
                doc.Close();
            }
            catch (Exception ex)
            {
                errorMessage += ex.Message + ",";
            }
        }
        #endregion

        ///// <summary>
        ///// 创建完成人确认表
        ///// </summary>
        ///// <param name="doc"></param>
        ///// <param name="project"></param>
        //private static void CreateAuthorList(Document doc, Project project)
        //{
        //    Paragraph pg = new Paragraph("完成人排序确认表", fontHei);
        //    pg.Alignment = Element.ALIGN_CENTER;
        //    pg.SpacingBefore = 40;
        //    doc.Add(pg);
        //    doc.Add(new Paragraph("            项目名称：", fontSong));
        //    doc.Add(new Chunk("\n"));
        //    PdfPTable table = new PdfPTable(7);
        //    table.AddCell(PDFHelper.CreateCell(new Chunk("序号")));
        //    table.AddCell(PDFHelper.CreateCell(new Chunk("姓名")));
        //    table.AddCell(PDFHelper.CreateCell(new Chunk("单位"), 3));
        //    table.AddCell(PDFHelper.CreateCell(new Chunk("职务")));
        //    table.AddCell(PDFHelper.CreateCell(new Chunk("签字")));

        //    float tdHeight = 30;
        //    for (int i = 0; i < 15; i++)
        //    {
        //        table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty)), tdHeight));
        //        table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty)), tdHeight));
        //        table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty), 3), tdHeight));
        //        table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty)), tdHeight));
        //        table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk(string.Empty)), tdHeight));
        //    }
        //BaseFont bfSongV = BaseFont.CreateFont(@"C:\WINDOWS\FONTS\simsun.ttc,0", BaseFont.IDENTITY_V, BaseFont.NOT_EMBEDDED);
        //Font fontSongV = new Font(bfSongV, 12);
        //table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(0, 0, 0, 0, 1, 1, new Paragraph("研发人员承诺", fontSongV), new Paragraph("未参与报奖的项目", fontSongV)), 100));
        //table.AddCell(PDFHelper.CreateCell(0, 0, 0, 0, 6, 1, new Paragraph("本人已知晓该项目报奖及其完成人排序，同意退出。", fontHei), new Paragraph("（所有人签同张纸上，如需代替，需在得到完成人认可后注明***代***。）", fontSong), new Paragraph("签字：", fontHei)));
        //table.AddCell(PDFHelper.SetHeight(PDFHelper.CreateCell(new Chunk("确认\n意见", fontSong), 15, 0, 0, 20), 80));
        //table.AddCell(PDFHelper.CreateCell(0, 0, 0, 0, 6, 1, new Paragraph("上述完成人排序已经项目组全体成员讨论通过，签字属实。", fontHei), new Paragraph("（项目负责人签字： ______________ ）", fontSong)));
        //    doc.Add(table);
        //}

        /// <summary>
        /// 用正则表达式匹配开始字符串和结束字符串之间的文本
        /// </summary>
        /// <param name="str">要匹配的文本</param>
        /// <param name="begin">开始字符串</param>
        /// <param name="end">结束字符串</param>
        /// <returns></returns>
        public static string GetInnerString(string str, string begin, string end)
        {
            string pattern = "(?<=(" + begin.Replace("\\", "\\\\") + "))[.\\s\\S]*?(?=(" + end.Replace("\\", "\\\\") + "))";
            try
            {
                return Regex.Match(str, pattern, RegexOptions.IgnoreCase).Value;
            }
            catch
            {
                return string.Empty;
            }
        }

        private static string GetEndString(string str, string begin)
        {
            string pattern = "(" + begin.Replace("\\", "\\\\") + "{.+})";
            try
            {
                return Regex.Match(str, pattern, RegexOptions.IgnoreCase).Value;
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
